package com.team24.hotel.service.impl;

import com.team24.hotel.domain.Hotel;
import com.team24.hotel.domain.Order;
import com.team24.hotel.mapper.HotelMapper;
import com.team24.hotel.mapper.OrderMapper;
import com.team24.hotel.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private HotelMapper hotelMapper;

    @Override
    public List<Order> findAll(){
        return orderMapper.selectAll();
    }

    @Override
    public Order findByPrimaryKey(String orderId) {
        return orderMapper.selectExtByPrimaryKey(orderId);
    }

    @Override
    public int saveOrUpdate(Order order) {
        if (StringUtils.isNotBlank(order.getOrderId())){
            return orderMapper.updateByPrimaryKeySelective(order);
        }else {
            return orderMapper.insert(order);
        }

    }

    @Override
    public int deleteOrder(String id) {
        return orderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateOrderStatusToCompleted(String id) {
        return orderMapper.updateOrderStatusToCompleted(id);
    }

    @Override
    public int updateOrderStatusToCancel(String id) {
        return orderMapper.updateOrderStatusToCancel(id);
    }

    @Override
    public int updateOrderStatusToNoPay(String id) {
        return orderMapper.updateOrderStatusToNoPay(id);
    }

    @Override
    public int updateOrderStatusToPayed(String id) {
        return orderMapper.updateOrderStatusToPayed(id);
    }

    @Override
    public List<Order> listHistoryOrder() {
        return orderMapper.selectHistoryAll();
    }

    @Override
    public int addOrderAndAddHot(Order order) {
        Hotel hotel = hotelMapper.selectByPrimaryKey(order.getHotelId());
        if(hotel.getHotelHot()==null) hotel.setHotelHot(0l);
        hotel.setHotelHot(hotel.getHotelHot()+1);
        hotelMapper.updateByPrimaryKeySelective(hotel);
        return orderMapper.bookOrder(order);
    }

}
